<?php
/* retargeting script to provide product offers 
	that the user seemed to be interested in or 
	products the user interacted with*/

// include the global config file
require_once("config.php");

// external lib: Smarty Template Engine
require_once("lib/Smarty/Smarty.class.php");

// external lib: Minify Classes incl. JSMin
require_once("lib/Minify/JSMin.class.php");
require_once("lib/Minify/Minify_HTML.class.php");
require_once("lib/Minify/Minify_Javascript.class.php");

// application internal classes
require_once("class/cache.class.php");
require_once("class/database.class.php");
require_once("class/blog.class.php");
require_once("class/amazon.class.php");
require_once("class/minify.class.php");

// instanciate the blog object
$b = new blog($_SERVER["HTTP_HOST"]);

/* product list will hold the 3 most relevant 
	products for the user and then query 
	amazon for similar products */
$productList = array();

if(array_key_exists("rtc", $_COOKIE)){
	$plist = array();
	$ilist = array();

	$rtcList = explode("|", $_COOKIE["rtc"]);
	for($r=0;$r<count($rtcList);$r++){
		$rtcItem = explode(":", $rtcList[$r]);
		array_push($plist, $rtcItem[0]);
		array_push($ilist, $rtcItem[1]);
	}

	// finally sort the plist
	array_multisort($ilist,SORT_DESC,$plist);
	
	$pcount = 0;
	while(($pcount < 3) && ($pcount < count($plist))){
		$productList[$pcount] = $plist[$pcount];
		$pcount++;
	}
}

// instanciate the amazon object
$lang = $b->db->getLanguageCode();
$amzn = new amazon($lang);
$similarities = $amzn->getSimilaritiesCached($productList);

$template = new Smarty();
$template->assign("similarities",$similarities);
$template->assign("languageCode",$lang);
$html = $template->fetch("themes/default/sidebarproduct.tpl");

// set the content type and charset header before output
header("Content-Type: text/html; charset=utf-8");

// finally print out the minified html code
print(minify::minifyHtml($html));
?>